#include <iomanip>
#include <sstream>
#include "DumpLog.h"
#include "TestFun.h"
#include "HelpFun.h"
#include "SolverFun.h"
#include "Statistics.h"

int main()
{

    //1. 设置函数
    auto fun    = Solver::Test::getFun(Solver::FunType_E::NIST_Gauss3_E);

    //2. 新建求解器
    auto solver = Solver::SolverFun::getSolver(Solver::SolverType_E::SWO_E);

    //3.设置迭代条件
    Solver::value_t minErr = 0;     //迭代终止目标,当目标函数值小于此值时停止迭代
    std::size_t iterSum    = 20000; //最大迭代次数

    //4.设置变量范围
    Solver::value_t minVar = -10000; //变量下限
    Solver::value_t maxVar = 10000;  //变量上限

    //5.设置种群数量
    std::size_t popNum = 90;

    //6.求解
    {
        //6.1 设置基本参数
        solver->setFun(fun);
        solver->setVarRange(minVar, maxVar, fun->getDim());
        solver->getCondition().limitError = minErr;
        solver->getCondition().limitIter  = iterSum;
        solver->setPrintNum(10);//间隔10次打印一次结果

        //6.2 求解
        solver->solve(popNum);
        solver->waitExit();

        //6.3 获取结果
        auto res = solver->getResult();

        //6.4 打印
        std::ostringstream ss;
        ss << std::setprecision(16);
        ss  << "\n------------------------------------------\n"
            << "使用求解器:"    << solver->getName()                           << "\n"
            << "函数等级  :"    << fun->getLevelStr()                          << "\n"
            << "最佳目标值:"    << fun->getBestY()                             << "\n"
            << "及格目标值:"    << fun->getPassY()                             << "\n"
            << "最终目标值:"    << solver->getResult()->y                      << "\n"
            << "最终优化值:[\n" << Help::printVecValue(solver->getResult()->x) << "]\n"
            << "------------------------------------------\n";
        
        LogInfo(ss.str());
    }

    return 0;
}